//
//  ShopDetailController.m
//  quxiangtao
//
//  Created by HHuajiang on 2018/12/18.
//  Copyright © 2018 shanhaijin. All rights reserved.
//

#import "ShopDetailController.h"
#import "ShopDetailViewCell.h"
#import "CreateShareViewController.h"

@interface ShopDetailController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *shopDetailTable;
@property (nonatomic, strong) NSArray *imageArray;
@property (nonatomic, strong) NSString *item_url;
@end

@implementation ShopDetailController

- (UITableView *)shopDetailTable {
    if (!_shopDetailTable) {
        _shopDetailTable = [[UITableView alloc] init];
        _shopDetailTable.backgroundColor = WhiteColor;
        _shopDetailTable.rowHeight = WIDTH+200;
        _shopDetailTable.delegate = self;
        _shopDetailTable.dataSource = self;
        _shopDetailTable.showsVerticalScrollIndicator = NO;
        _shopDetailTable.showsHorizontalScrollIndicator = NO;
        _shopDetailTable.estimatedRowHeight = 0;
        _shopDetailTable.estimatedSectionFooterHeight = 0;
        _shopDetailTable.estimatedSectionHeaderHeight = 0;
        _shopDetailTable.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_shopDetailTable registerClass:[ShopDetailViewCell class] forCellReuseIdentifier:@"ShopDetailViewCell"];
    }
    return _shopDetailTable;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"商品详情";
    self.view.backgroundColor = WhiteColor;
    
    UIView *bottomView = [[UIView alloc] init];
    bottomView.backgroundColor = WhiteColor;
    [self.view addSubview:bottomView];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(IS_IPHONE_X?-20:0);
        make.height.mas_equalTo(jisuan(100));
    }];
    
    [self.view addSubview:self.shopDetailTable];
    [self.shopDetailTable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(0);
        make.bottom.equalTo(bottomView.mas_top);
    }];
    
    UIView *linevvv = [[UIView alloc] init];
    linevvv.backgroundColor = Colorcccccc;
    [bottomView addSubview:linevvv];
    [linevvv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.height.mas_equalTo(0.5);
    }];
    
    UIButton *shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
    shareButton.backgroundColor = WhiteColor;
    [shareButton setTitle:@"分享赚钱" forState:UIControlStateNormal];
    [shareButton setTitleColor:Color000000 forState:UIControlStateNormal];
    shareButton.titleLabel.font = [UIFont boldSystemFontOfSize:17];
    [bottomView addSubview:shareButton];
    [shareButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.mas_equalTo(0);
        make.top.mas_equalTo(0.5);
        make.width.mas_equalTo(WIDTH/2);
    }];
    
    UIButton *saveMoneyButton = [UIButton buttonWithType:UIButtonTypeCustom];
    saveMoneyButton.backgroundColor = Colorff0000;
    [saveMoneyButton setTitleColor:WhiteColor forState:UIControlStateNormal];
    [saveMoneyButton setTitle:@"省钱购买" forState:UIControlStateNormal];
    saveMoneyButton.titleLabel.font = [UIFont boldSystemFontOfSize:17];
    [bottomView addSubview:saveMoneyButton];
    [saveMoneyButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.mas_equalTo(0);
        make.top.mas_equalTo(0.5);
        make.width.mas_equalTo(WIDTH/2);
    }];
    
    [saveMoneyButton addTarget:self action:@selector(pushToTripartite) forControlEvents:UIControlEventTouchUpInside];
    
    [shareButton addTarget:self action:@selector(shareButtonClick) forControlEvents:UIControlEventTouchUpInside];
    
    [self getShoppingDetailData];
}

- (void)getShoppingDetailData {
    WS(weakSelf);
    [QNetWorkRequest taoBaoShopDetailarameter:@{@"item_id":self.model.item_id} Success:^(id response) {
        weakSelf.imageArray = response[@"data"][@"results"][@"n_tbk_item"][@"small_images"][@"string"];
        weakSelf.item_url = response[@"data"][@"results"][@"n_tbk_item"][@"h5url"];
        [weakSelf.shopDetailTable reloadData];
    } Failure:^(NSError *error) {
    }];
}

- (void)shareButtonClick {
    CreateShareViewController *shareVc = [[CreateShareViewController alloc] init];
    shareVc.imageArry =  self.imageArray;
    shareVc.model = self.model;
    shareVc.item_url = self.item_url;
    [self.navigationController pushViewController:shareVc animated:YES];
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    WS(weakSelf);
    ShopDetailViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ShopDetailViewCell"];
    cell.model = self.model;
    cell.imageArray = self.imageArray;
    cell.getButtonClickBlock = ^{
        [weakSelf pushToTripartite];
    };
    return cell;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

#pragma 跳转到 淘宝
- (void)pushToTripartite {
    [[CommonAction sharedInstance] goToTbGoodsDetailWith:self itemDetailPage:self.model.coupon_click_url];
}



@end
